package com.examples.gateway.helper.filter;

import com.examples.gateway.common.CheckState;
import com.examples.gateway.entity.SystemPermission;
import com.examples.gateway.helper.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 如果是开发路由不做后续的校验
 * @author Dai YaLu
 */
@Slf4j
public class DeveloperRouterFilter extends AbstractGlobalHelperFilter{

    private final String PREFIX = "DEV_";
    @Override
    public Mono<Void> doFilter(ServerWebExchange exchange, GatewayFilterChain chain) {
        SystemPermission permission = exchange.getAttribute(Constants.EXAMPLES_GATEWAY_CHECK_PERMISSION);
        if (null != permission && permission.getCode().startsWith(PREFIX)) {
            exchange.getAttributes().put(Constants.EXAMPLES_GATEWAY_CHECK_STATE, CheckState.SUCCESS_DEVELOPER_ROUTE);
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 91;
    }
}
